package com.zhiche.lisa.oms.service.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import com.zhiche.lisa.oms.dto.TransportationTypeDTO;
import com.zhiche.lisa.oms.vo.LocationTypeVo;
import com.zhiche.lisa.oms.vo.ServiceTypeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class MdmUtil {

    private static Logger logger = LoggerFactory.getLogger(MdmUtil.class);

    @Value("${lisa.mdm.getLocationTypeByValueUrl}")
    private String getLocationTypeByValueUrl;

    @Value("${lisa.mdm.getLocationTypeByIdUrl}")
    private String getLocationTypeByIdUrl;

    //连接超时时间
    @Value("${lisa.socketTimeout}")
    private int socketTimeout;

    @Value("${lisa.mdm.getAllServiceTypeUrl}")
    private String getAllServiceTypeUrl;

    @Value("${lisa.mdm.getAllTransTypeUrl}")
    private String getAllTransTypeUrl;

    /**
     * @param typeValue
     * @return
     */
    public LocationTypeVo getLocationTypeByValue(String typeValue) {
        LocationTypeVo locationTypeVo = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("typeValue", typeValue));
            String strReturn = HttpClientUtil.get(getLocationTypeByValueUrl, headNamePairs, params, socketTimeout);
            if (!org.springframework.util.StringUtils.isEmpty(strReturn)) {
                RestfulResponse<LocationTypeVo> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LocationTypeVo>>() {
                });
                if (!Objects.isNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    locationTypeVo = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return locationTypeVo;
    }

    /**
     * @param id
     * @return
     */
    public LocationTypeVo getLocationTypeById(Long id) {
        LocationTypeVo locationTypeVo = null;
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            List<NameValuePair> params = Lists.newArrayList();
            params.add(new BasicNameValuePair("id", id.toString()));
            String strReturn = HttpClientUtil.get(getLocationTypeByIdUrl, headNamePairs, params, socketTimeout);
            if (!org.springframework.util.StringUtils.isEmpty(strReturn)) {
                RestfulResponse<LocationTypeVo> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LocationTypeVo>>() {
                });
                if (!Objects.isNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    locationTypeVo = restfulResponse.getData();
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return locationTypeVo;
    }

    /**
     * 获取全部服务类型
     *
     * @return
     */
    public List<ServiceTypeVO> getAllServiceTypeList() {
        try {
            List<NameValuePair> headNamePairs = Lists.newArrayList();
            headNamePairs.add(new BasicNameValuePair("Authorization", ""));
            String strReturn = HttpClientUtil.get(getAllServiceTypeUrl, headNamePairs, socketTimeout);
            if (StringUtils.isNotEmpty(strReturn)) {
                RestfulResponse<List<ServiceTypeVO>> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<ServiceTypeVO>>>() {
                });
                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
                    List<ServiceTypeVO> listServiceType = restfulResponse.getData();
                    if (CollectionUtils.isNotEmpty(listServiceType)) {
                        return listServiceType;
                    }
                }
            }
        } catch (Exception ex) {
            throw new BaseException(ex.getMessage());
        }
        return null;
    }

    /**
     * 获取所有运输工具
     */
    public List<TransportationTypeDTO> getAllTransType() {
        try {
            String strReturn = HttpClientUtil.get(getAllTransTypeUrl, null, socketTimeout);
            if (StringUtils.isNotBlank(strReturn)) {
                RestfulResponse<List<TransportationTypeDTO>> response = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<List<TransportationTypeDTO>>>() {
                });
                if (response != null && CollectionUtils.isNotEmpty(response.getData())) {
                    return response.getData();
                }
            }
        } catch (Exception e) {
            logger.error("连接获取所有运输方式失败 error:{}", e);
        }
        return null;
    }

}
